package edu.jd.movieplatform.user.order.impl;

import edu.jd.movieplatform.user.order.OrderDto;
import edu.jd.movieplatform.user.order.UserOrderService;
import edu.jd.movieplatform.user.order.dao.OrderDao;
import edu.jd.movieplatform.user.ticket.Ticket;
import edu.jd.movieplatform.user.ticket.TicketDto;
import edu.jd.movieplatform.user.ticket.TicketService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements UserOrderService {

    @Resource
    private OrderDao orderdao;

    @Resource
    private TicketService ticketService;

    @Override
    public List<TicketDto> submitOrder(OrderDto[] dtoList) {
        int maxOrder_num = orderdao.getMaxOrder_num();
        int newOrder_num = maxOrder_num + 1;
        for(OrderDto dto : dtoList){
            dto.setOrder_num(newOrder_num);
        }
        orderdao.insertOrder(newOrder_num, dtoList[0].getCust_username(), dtoList[0].getOrder_time());
        /*for(OrderDto dto : dtoList){
            dto.setOrder_num(newOrder_num);
            TicketDto ticketDto = ticketService.generateTicket(dto);
            tickList.add(ticketDto);
        }*/

        return ticketService.generateTicket(dtoList, newOrder_num);
    }
}
